package edu.ccut.saturn.studio.dialog.webcontextdialog;

import org.eclipse.jface.dialogs.IDialogConstants;
import org.eclipse.jface.dialogs.TitleAreaDialog;
import org.eclipse.jface.window.Window;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.events.MouseAdapter;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.events.MouseTrackAdapter;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.layout.FormAttachment;
import org.eclipse.swt.layout.FormData;
import org.eclipse.swt.layout.FormLayout;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
import org.eclipse.ui.dialogs.ElementListSelectionDialog;

import edu.ccut.saturn.studio.Activator;
import edu.ccut.saturn.studio.Images;
import edu.ccut.saturn.studio.core.util.eclipse.Messages;
import edu.ccut.saturn.studio.core.util.eclipse.SaturnSWTUtils;
import edu.ccut.saturn.studio.dialog.SelectionCandidate;
import edu.ccut.saturn.studio.logic.model.saturn.SaturnWebContextNode;
import edu.ccut.saturn.studio.logic.model.saturn.SaturnWebContextParameter;
import edu.ccut.saturn.studio.util.ConversionUtil;

public class WebContextDialogForEdit extends TitleAreaDialog {
	
	private Shell shell;

	private Text IDText, keyText, newKeyText;
	
	private Label IDLabel, scopeLabel, keyLabel, imageLabel, newScopeLabel, newKeyLabel;
	
	private Combo scopeCombo, newScopeCombo;
	
	private Button deleteYes, deleteNo;
	
	private SaturnWebContextNode saturnWebContextNode;
	
	private SaturnWebContextParameter parameter;
	
	public WebContextDialogForEdit(Shell parentShell, SaturnWebContextParameter parameter, SaturnWebContextNode saturnWebContextNode) {
		super(parentShell);
		this.shell = parentShell;
		this.parameter = parameter;
		this.saturnWebContextNode = saturnWebContextNode;
	}
	
	protected Control createContents(Composite parent) {
		Control contents = super.createContents(parent);
		setMessage(Messages.getString("WebContextDialogForEdit.EditContextPara")); //$NON-NLS-1$
		return contents;
	}
	
	@Override
	protected Control createDialogArea(Composite parent) {
		
		getShell().setText("WebContextDialogForEdit.1");
		setTitleImage(Activator.getImage(Images.SATURN_ICONS_MAXNAME));
		Composite area = (Composite) super.createDialogArea(parent);

		final Composite composite = new Composite(area, SWT.NONE);
		composite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
		final GridLayout gridLayout = new GridLayout();
		gridLayout.numColumns = 3;
		composite.setLayout(gridLayout);

		IDLabel = new Label(composite, SWT.NONE);
		IDLabel.setText(Messages.getString("WebContextDialogForEdit.ContextID")); //$NON-NLS-1$

		IDText = new Text(composite, SWT.BORDER | SWT.READ_ONLY);
		final GridData gd_iDText = new GridData(SWT.FILL, SWT.FILL, true, false, 2, 1);
		IDText.setLayoutData(gd_iDText);
		IDText.setText(parameter.getId().trim());

		scopeLabel = new Label(composite, SWT.NONE);
		scopeLabel.setLayoutData(new GridData());
		scopeLabel.setText(Messages.getString("WebContextDialogForEdit.ContextScope")); //$NON-NLS-1$

		scopeCombo = new Combo(composite, SWT.READ_ONLY);
		final GridData gd_scopeCombo = new GridData(SWT.FILL, SWT.CENTER, true, false, 2, 1);
		scopeCombo.setLayoutData(gd_scopeCombo);
		scopeCombo.setItems(SelectionCandidate.scopes);
		scopeCombo.setText(parameter.getScope().trim());
		scopeCombo.addModifyListener(new ModifyListener() {
			public void modifyText(final ModifyEvent e) {
				keyText.setText("");
			}
		});

		keyLabel = new Label(composite, SWT.NONE);
		keyLabel.setLayoutData(new GridData());
		keyLabel.setText(Messages.getString("WebContextDialogForEdit.ContextKeyValue")); //$NON-NLS-1$

		keyText = new Text(composite, SWT.BORDER);
		final GridData gd_keyText = new GridData(SWT.FILL, SWT.CENTER, true, false);
		keyText.setLayoutData(gd_keyText);
		keyText.setText(parameter.getKey().trim());
		
		imageLabel = new Label(composite, SWT.NONE);
		imageLabel.setImage(Activator.getImage(Images.AMPLIFY_PLUS));
		imageLabel.setLayoutData(new GridData());
		imageLabel.addMouseTrackListener(new MouseTrackAdapter() {
			public void mouseEnter(final MouseEvent e) {
				imageLabel.setImage(Activator.getImage(Images.AMPLIFY_MINUS));
			}

			public void mouseExit(final MouseEvent e) {
				imageLabel.setImage(Activator.getImage(Images.AMPLIFY_PLUS));
			}
		});
		imageLabel.addMouseListener(new MouseAdapter() {
			public void mouseDown(final MouseEvent e) {
				ElementListSelectionDialog dialog1 = new ElementListSelectionDialog(shell, 	new SaturnSWTUtils.SaturnLabelDefaultProvider());
				dialog1.setTitle(Messages.getString("WebContextDialogForEdit.ChooseKeyValue")); //$NON-NLS-1$
				dialog1.setElements(saturnWebContextNode.getValueKeyByScope(ConversionUtil.switchScopeToDigital(scopeCombo.getText().trim())));
				if(dialog1.open() == Window.OK){
					keyText.setText((String) dialog1.getFirstResult());
				}
			}
		});

		final Group group = new Group(composite, SWT.NONE);
		group.setLayout(new FormLayout());
		group.setText(Messages.getString("WebContextDialogForEdit.IfDelete")); //$NON-NLS-1$
		final GridData gd_group = new GridData(SWT.LEFT, SWT.CENTER, false, false, 3, 1);
		gd_group.heightHint = 25;
		gd_group.widthHint = 150;
		group.setLayoutData(gd_group);

		deleteYes = new Button(group, SWT.RADIO);
		deleteYes.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(final SelectionEvent e) {
				if(deleteYes.getSelection() == true){
					newScopeCombo.setEnabled(false);
					newKeyText.setEnabled(false);
				}
			}
		});
		final FormData fd_deleteYes = new FormData();
		fd_deleteYes.bottom = new FormAttachment(0, 21);
		fd_deleteYes.top = new FormAttachment(0, 5);
		fd_deleteYes.right = new FormAttachment(0, 38);
		fd_deleteYes.left = new FormAttachment(0, 10);
		deleteYes.setLayoutData(fd_deleteYes);
		deleteYes.setText(Messages.getString("WebContextDialogForEdit.yes")); //$NON-NLS-1$

		deleteNo = new Button(group, SWT.RADIO);
		deleteNo.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(final SelectionEvent e) {
				newScopeCombo.setEnabled(true);
				newKeyText.setEnabled(true);
			}
		});
		final FormData fd_deleteNo = new FormData();
		fd_deleteNo.left = new FormAttachment(deleteYes, 35, SWT.RIGHT);
		fd_deleteNo.bottom = new FormAttachment(0, 21);
		fd_deleteNo.top = new FormAttachment(0, 5);
		deleteNo.setLayoutData(fd_deleteNo);
		deleteNo.setText(Messages.getString("WebContextDialogForEdit.no")); //$NON-NLS-1$

		newScopeLabel = new Label(composite, SWT.NONE);
		newScopeLabel.setText(Messages.getString("WebContextDialogForEdit.NewScope")); //$NON-NLS-1$
		
		newScopeCombo = new Combo(composite, SWT.READ_ONLY);
		final GridData gd_newScopeText = new GridData(SWT.FILL, SWT.CENTER, true, false, 2, 1);
		newScopeCombo.setLayoutData(gd_newScopeText);
		newScopeCombo.setItems(SelectionCandidate.scopes);

		newKeyLabel = new Label(composite, SWT.NONE);
		newKeyLabel.setText(Messages.getString("WebContextDialogForEdit.NewKeyValue")); //$NON-NLS-1$

		newKeyText = new Text(composite, SWT.BORDER);
		final GridData gd_newKeyText = new GridData(SWT.FILL, SWT.CENTER, true, false, 2, 1);
		newKeyText.setLayoutData(gd_newKeyText);
		new Label(composite, SWT.NONE);
		
		if("true".equals(parameter.getIsDelete())){
			deleteYes.setSelection(true);
			newScopeCombo.setEnabled(false);
			newKeyText.setEnabled(false);
		}if("false".equals(parameter.getIsDelete())){
			deleteNo.setSelection(true);
			newScopeCombo.setEnabled(true);
			newKeyText.setEnabled(true);
		}
		newKeyText.setText(parameter.getNewKey().trim());
		newScopeCombo.setText(parameter.getNewScope().trim());
		
		return area;
	}
	
	@Override
	protected void buttonPressed(int buttonId) {
		if (buttonId == IDialogConstants.OK_ID) {
			parameter.setId(IDText.getText().trim());
			parameter.setKey(keyText.getText().trim());
			parameter.setScope(scopeCombo.getText().trim());
			if(deleteYes.getSelection()==true){
				parameter.setIsDelete("true");
			}if(deleteNo.getSelection()==true){
				parameter.setIsDelete("false");
			}
			parameter.setNewKey(newKeyText.getText().trim());
			parameter.setNewScope(newScopeCombo.getText().trim());
		}
		super.buttonPressed(buttonId);
	}
	
	@Override
	protected Point getInitialSize() {
		return new Point(460, 320);
	}
	
	protected void configureShell(Shell newShell) {
		super.configureShell(newShell);
		newShell.setImage(Activator.getImage(Images.SATURN_ICONS_SMALLNAME));
	}
}